﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using Maticsoft.Common;

namespace Maticsoft.Web.wap
{
    public partial class ProductDetail : BasePage
    {
        int ProductId;
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxProcess();

            int.TryParse(Request.QueryString["id"], out ProductId);
            hidProductId.Value = ProductId.ToString();

            if (!IsPostBack)
            {
                GetProduct();
                GetProductPic();
                BindComments();
            }
        }

        protected void AjaxProcess( )
        {
            if (Request.QueryString["ope"] == "addnum")
            {
                Response.Write(AddNum());
                Response.End();
            }
            else if (Request.QueryString["ope"] == "carnum")
            {
                Response.Write(CarNum());
                Response.End();
            }
        }

        protected string CarNum( )
        {
            BLL.ShoppingInfo carBLL = new BLL.ShoppingInfo();
            int recordCount = carBLL.GetRecordCount("UserId=" + DigitHelper.GetInt32(this.UserId) + " and IsActive=1");

            return recordCount.ToString();
        }

        protected string AddNum( )
        {
            int productId = 0;
            int num = 0;
            int.TryParse(Request.QueryString["productId"], out productId);
            int.TryParse(Request.QueryString["num"], out num);
            if (productId > 0)
            {
                if (string.IsNullOrEmpty(this.UserId))
                {
                    return "-5";
                }

                BLL.Products proBLL = new BLL.Products();
                Model.Products proModel = proBLL.GetModel(productId);
                BLL.ShoppingInfo carBLL = new BLL.ShoppingInfo();
                Model.ShoppingInfo carModel = new Model.ShoppingInfo();
                carModel.UserId = int.Parse(this.UserId);
                carModel.ProductId = productId;
                carModel.ProductNum = num;
                carModel.ProductDesc = Request.QueryString["lay"];
                carModel.IsActive = 1;
                carModel.CreateTime = DateTime.Now;

                return carBLL.AddShoppingInfo(carModel).ToString();
            }

            return "0";
        }

        protected void GetProduct( )
        {
            BLL.Products proBLL = new BLL.Products();
            Model.Products proModel = proBLL.GetModel(ProductId);
            ltlProductName.Text = proModel.ProductName;
            ltlSalePrice.Text = proModel.SalePrice.ToString();
            ltlDetail.Text = proModel.DetailWap;
            ltlNum.Text = proModel.ProductNo;
            ltlWeight.Text = proModel.Weight + "克";
            if (!string.IsNullOrEmpty(proModel.Layout))
            {
                StringBuilder strBuilder = new StringBuilder();
                string[] arr = proModel.Layout.Split('|');
                foreach (string s in arr)
                {
                    strBuilder.Append("<span onclick=\"SelLayout(this);\">" + s + "</span>");
                }
                ltlLay.Text = strBuilder.ToString();
            }
            
        }

        protected void GetProductPic( )
        {
            BLL.ProductPics picBLL = new BLL.ProductPics();
            DataSet ds = picBLL.GetList("productId=" + ProductId + " and IsActive=1");
            StringBuilder builerPic = new StringBuilder();
            StringBuilder builderDiv=new StringBuilder();
            int recordcount=ds.Tables[0].Rows.Count;
            builerPic.Append("<div class=\"mui-slider-item mui-slider-item-duplicate\">");
            builerPic.Append("<a href=\"javascript:;\"><img src=\"/uploadpic/" + ds.Tables[0].Rows[0]["url"] + "\"></a>");
            builerPic.Append("</div>");
            for (int i = 0; i < recordcount;i++ )
            {
                builerPic.Append("<div class=\"mui-slider-item\">");
                builerPic.Append("<a href=\"javascript:;\"><img src=\"/uploadpic/" + ds.Tables[0].Rows[i]["url"] + "\"></a>");
                builerPic.Append("</div>");

                if (i == 0)
                {
                    builderDiv.Append("<div class=\"mui-indicator mui-active\"></div>");
                }
                else
                {
                    builderDiv.Append("<div class=\"mui-indicator\"></div>");
                }
            }
            builerPic.Append("<div class=\"mui-slider-item mui-slider-item-duplicate\">");
            builerPic.Append("<a href=\"javascript:;\"><img src=\"/uploadpic/" + ds.Tables[0].Rows[0]["url"] + "\"></a>");
            builerPic.Append("</div>");
            proPic.InnerHtml = builerPic.ToString();
            proPicDiv.InnerHtml = builderDiv.ToString();
        }

        protected void BindComments( )
        {
            BLL.Comments comBLL = new BLL.Comments();
            string strWhere="productId=" + ProductId + " and IsActive=1 and ShowStatus=1";
            int recordCount = comBLL.GetRecordCount(strWhere);
            ltlCount.Text = recordCount.ToString();
            DataSet ds = comBLL.GetListByPageV(strWhere, "CommentId desc", 1, 4);
        }

        protected string GetStar( object obj )
        {
            int points = 0;
            int.TryParse(Convert.ToString(obj), out points);
            StringBuilder sb = new StringBuilder();
            for (int i = 1; i <= points; i++)
            {
                sb.Append("<img class=\"mui-pull-left starbox\" src=\"img/xing.png\">");
            }

            return sb.ToString();
        }
    }
}